<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="订单编号" prop="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单创建时间" prop="createTime">
        <el-date-picker clearable
          v-model="queryParams.createTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择订单创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="计划质量/t" prop="plannedProduction">
        <el-input
          v-model="queryParams.plannedProduction"
          placeholder="请输入计划质量/t"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="当前质量/t" prop="currentOutput">
        <el-input
          v-model="queryParams.currentOutput"
          placeholder="请输入当前质量/t"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="钢种规格" prop="steelSpec">
        <el-input
          v-model="queryParams.steelSpec"
          placeholder="请输入钢种规格，如20mm、18mm等"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="成材率" prop="yieldRate">
        <el-input
          v-model="queryParams.yieldRate"
          placeholder="请输入成材率"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="热装率" prop="hotChargingRate">
        <el-input
          v-model="queryParams.hotChargingRate"
          placeholder="请输入热装率"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="废品率" prop="scrapRate">
        <el-input
          v-model="queryParams.scrapRate"
          placeholder="请输入废品率"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库存率" prop="inventoryRate">
        <el-input
          v-model="queryParams.inventoryRate"
          placeholder="请输入库存率"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预计完成时间" prop="estCompTime">
        <el-date-picker clearable
          v-model="queryParams.estCompTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择预计完成时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:main:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:main:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:main:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:main:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="mainList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="工厂ID" align="center" prop="companyId" />
      <el-table-column label="订单编号/任务编号" align="center" prop="orderCode" />
      <el-table-column label="订单年份" align="center" prop="orderYear" />
      <el-table-column label="订单月份" align="center" prop="orderMonth" />
      <el-table-column label="计划质量/t" align="center" prop="plannedProduction" />
      <el-table-column label="当前质量/t" align="center" prop="currentOutput" />
      <el-table-column label="钢种规格" align="center" prop="steelSpec" />
      <el-table-column label="成材率" align="center" prop="yieldRate" />
      <el-table-column label="热装率" align="center" prop="hotChargingRate" />
      <el-table-column label="废品率" align="center" prop="scrapRate" />
      <el-table-column label="库存率" align="center" prop="inventoryRate" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="订单状态" align="center" prop="orderStatus" />
      <el-table-column label="预计完成时间" align="center" prop="estCompTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.estCompTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:main:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:main:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单/任务主对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单编号/任务编号" prop="orderCode">
          <el-input v-model="form.orderCode" placeholder="请输入订单编号/任务编号" />
        </el-form-item>
        <el-form-item label="订单年份" prop="orderYear">
          <el-input v-model="form.orderYear" placeholder="请输入订单年份" />
        </el-form-item>
        <el-form-item label="订单月份" prop="orderMonth">
          <el-input v-model="form.orderMonth" placeholder="请输入订单月份" />
        </el-form-item>
        <el-form-item label="计划质量/t" prop="plannedProduction">
          <el-input v-model="form.plannedProduction" placeholder="请输入计划质量/t" />
        </el-form-item>
        <el-form-item label="当前质量/t" prop="currentOutput">
          <el-input v-model="form.currentOutput" placeholder="请输入当前质量/t" />
        </el-form-item>
        <el-form-item label="钢种规格" prop="steelSpec">
          <el-input v-model="form.steelSpec" placeholder="请输入钢种规格，如20mm、18mm等" />
        </el-form-item>
        <el-form-item label="成材率" prop="yieldRate">
          <el-input v-model="form.yieldRate" placeholder="请输入成材率" />
        </el-form-item>
        <el-form-item label="热装率" prop="hotChargingRate">
          <el-input v-model="form.hotChargingRate" placeholder="请输入热装率" />
        </el-form-item>
        <el-form-item label="废品率" prop="scrapRate">
          <el-input v-model="form.scrapRate" placeholder="请输入废品率" />
        </el-form-item>
        <el-form-item label="库存率" prop="inventoryRate">
          <el-input v-model="form.inventoryRate" placeholder="请输入库存率" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="预计完成时间" prop="estCompTime">
          <el-date-picker clearable
            v-model="form.estCompTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择预计完成时间">
          </el-date-picker>
        </el-form-item>
        <el-divider content-position="center">订单/任务详情信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddOrderDetail">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteOrderDetail">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="orderDetailList" :row-class-name="rowOrderDetailIndex" @selection-change="handleOrderDetailSelectionChange" ref="orderDetail">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="当前质量/t" prop="totalQuantity" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.totalQuantity" placeholder="请输入当前质量/t" />
            </template>
          </el-table-column>
          <el-table-column label="生产日/天" prop="productionDay" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.productionDay" placeholder="请输入生产日/天" />
            </template>
          </el-table-column>
          <el-table-column label="备注" prop="remark" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.remark" placeholder="请输入备注" />
            </template>
          </el-table-column>
          <el-table-column label="是否废品" prop="isScrap" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.isScrap" placeholder="请输入是否废品：0:否，1:是" />
            </template>
          </el-table-column>
          <el-table-column label="钢坯编号" prop="billetCode" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.billetCode" placeholder="请输入钢坯编号" />
            </template>
          </el-table-column>
          <el-table-column label="备用字段1" prop="spareField1" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.spareField1" placeholder="请输入备用字段1" />
            </template>
          </el-table-column>
          <el-table-column label="备用字段2" prop="spareField2" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.spareField2" placeholder="请输入备用字段2" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMain, getMain, delMain, addMain, updateMain } from "@/api/businessInfo/orderMain";

export default {
  name: "Main",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedOrderDetail: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单/任务主表格数据
      mainList: [],
      // 订单/任务详情表格数据
      orderDetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyId: null,
        orderCode: null,
        orderYear: null,
        orderMonth: null,
        plannedProduction: null,
        currentOutput: null,
        steelSpec: null,
        yieldRate: null,
        hotChargingRate: null,
        scrapRate: null,
        inventoryRate: null,
        orderStatus: null,
        estCompTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderYear: [
          { required: true, message: "订单年份不能为空", trigger: "blur" }
        ],
        orderMonth: [
          { required: true, message: "订单月份不能为空", trigger: "blur" }
        ],
        orderStatus: [
          { required: true, message: "订单状态 1:未开始 2:进行中 3:已完成不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单/任务主列表 */
    getList() {
      this.loading = true;
      listMain(this.queryParams).then(response => {
        this.mainList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        companyId: null,
        orderCode: null,
        orderYear: null,
        orderMonth: null,
        plannedProduction: null,
        currentOutput: null,
        steelSpec: null,
        yieldRate: null,
        hotChargingRate: null,
        scrapRate: null,
        inventoryRate: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        orderStatus: null,
        estCompTime: null
      };
      this.orderDetailList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单/任务主";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMain(id).then(response => {
        this.form = response.data;
        this.orderDetailList = response.data.orderDetailList;
        this.open = true;
        this.title = "修改订单/任务主";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.orderDetailList = this.orderDetailList;
          if (this.form.id != null) {
            updateMain(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMain(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单/任务主编号为"' + ids + '"的数据项？').then(function() {
        return delMain(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 订单/任务详情序号 */
    rowOrderDetailIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 订单/任务详情添加按钮操作 */
    handleAddOrderDetail() {
      let obj = {};
      obj.totalQuantity = "";
      obj.productionDay = "";
      obj.isDelete = "";
      obj.remark = "";
      obj.isScrap = "";
      obj.billetCode = "";
      obj.spareField1 = "";
      obj.spareField2 = "";
      this.orderDetailList.push(obj);
    },
    /** 订单/任务详情删除按钮操作 */
    handleDeleteOrderDetail() {
      if (this.checkedOrderDetail.length == 0) {
        this.$modal.msgError("请先选择要删除的订单/任务详情数据");
      } else {
        const orderDetailList = this.orderDetailList;
        const checkedOrderDetail = this.checkedOrderDetail;
        this.orderDetailList = orderDetailList.filter(function(item) {
          return checkedOrderDetail.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleOrderDetailSelectionChange(selection) {
      this.checkedOrderDetail = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/main/export', {
        ...this.queryParams
      }, `main_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
